/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "FCLinkLabel.h"

namespace FaceCat{
    Long FCLinkLabel::getPaintingLinkColor(){
        if (isEnabled()){
            FCNative *native = getNative();
            if(this == native->getHoveredView()){
                return m_activeLinkColor;
            }
            else if(this == native->getPushedView()){
                return m_activeLinkColor;
            }
            else{
                if (m_linkVisited && m_visited){
                    return m_visitedLinkColor;
                }
                else{
                    return m_linkColor;
                }
            }
        }
        else{
            return m_disabledLinkColor;
        }
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    FCLinkLabel::FCLinkLabel(){
        m_activeLinkColor = FCColor::rgb(255, 0, 0);
        m_disabledLinkColor = FCColor::rgb(133, 133, 133);
        m_linkBehavior = LinkBehaviorA_AlwaysUnderLine;
        m_linkColor = FCColor::rgb(0, 0, 255);
        m_linkVisited = false;
        m_visited = false;
        m_visitedLinkColor = FCColor::rgb(128, 0, 128);
        setCursor(FCCursors_Hand);
    }
    
    FCLinkLabel::~FCLinkLabel(){
    }
    
    Long FCLinkLabel::getActiveLinkColor(){
        return m_activeLinkColor;
    }
    
    void FCLinkLabel::setActiveLinkColor(Long value){
        m_activeLinkColor = value;
    }
    
    Long FCLinkLabel::getDisabledLinkColor(){
        return m_disabledLinkColor;
    }
    
    void FCLinkLabel::setDisabledLinkColor(Long value){
        m_disabledLinkColor = value;
    }
    
    FCLinkBehavior FCLinkLabel::getLinkBehavior(){
        return m_linkBehavior;
    }
    
    void FCLinkLabel::setLinkBehavior(FCLinkBehavior value){
        m_linkBehavior = value;
    }
    
    Long FCLinkLabel::getLinkColor(){
        return m_linkColor;
    }
    
    void FCLinkLabel::setLinkColor(Long value){
        m_linkColor = value;
    }
    
    bool FCLinkLabel::isLinkVisited(){
        return m_linkVisited;
    }
    
    void FCLinkLabel::setLinkVisited(bool value){
        m_linkVisited = value;
    }
    
    Long FCLinkLabel::getVisitedLinkColor(){
        return m_visitedLinkColor;
    }
    
    void FCLinkLabel::setVisitedLinkColor(Long value){
        m_visitedLinkColor = value;
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    String FCLinkLabel::getViewType(){
        return L"LinkLabel";
    }
    
    void FCLinkLabel::getAttribute(const String& name, String *value, String *type){
        if (name == L"activelinkcolor"){
            *type = L"color";
            *value = FCTran::colorToStr(getActiveLinkColor());
        }
        else if (name == L"disabledlinkcolor"){
            *type = L"color";
            *value = FCTran::colorToStr(getDisabledLinkColor());
        }
        else if (name == L"linkbehavior"){
            *type = L"enum:FCLinkBehavior";
            FCLinkBehavior linkBehavior = getLinkBehavior();
            if (linkBehavior == LinkBehaviorA_AlwaysUnderLine){
                *value = L"AlwaysUnderLine";
            }
            else if (linkBehavior == LinkBehaviorA_HoverUnderLine){
                *value = L"HoverUnderLine";
            }
            else{
                *value = L"NeverUnderLine";
            }
        }
        else if (name == L"linkcolor"){
            *type = L"color";
            *value = FCTran::colorToStr(getLinkColor());
        }
        else if (name == L"linkvisited"){
            *type = L"bool";
            *value = FCTran::boolToStr(isLinkVisited());
        }
        else if (name == L"visitedlinkcolor"){
            *type = L"color";
            *value = FCTran::colorToStr(getVisitedLinkColor());
        }
        else{
            FCLabel::getAttribute(name, value, type);
        }
    }
    
    ArrayList<String> FCLinkLabel::getAttributeNames(){
        ArrayList<String> attributeNames = FCLabel::getAttributeNames();
        attributeNames.add(L"ActiveLinkColor");
        attributeNames.add(L"DisabledLinkColor");
        attributeNames.add(L"LinkBehavior");
        attributeNames.add(L"LinkColor");
        attributeNames.add(L"LinkVisited");
        attributeNames.add(L"VisitedLinkColor");
        return attributeNames;
    }
    
    void FCLinkLabel::onClick(FCTouchInfo touchInfo){
        FCLabel::onClick(touchInfo);
        if (m_linkVisited){
            m_visited = true;
        }
    }
    
    void FCLinkLabel::onTouchDown(FCTouchInfo touchInfo){
        FCLabel::onTouchDown(touchInfo);
        invalidate();
    }
    
    void FCLinkLabel::onTouchEnter(FCTouchInfo touchInfo){
        FCLabel::onTouchEnter(touchInfo);
        invalidate();
    }
    
    void FCLinkLabel::onTouchLeave(FCTouchInfo touchInfo){
        FCLabel::onTouchLeave(touchInfo);
        invalidate();
    }
    
    void FCLinkLabel::onTouchUp(FCTouchInfo touchInfo){
        FCLabel::onTouchUp(touchInfo);
        invalidate();
    }
    
    void FCLinkLabel::onPaintForeground(FCPaint *paint, const FCRect& clipRect){
        String text = getText();
        if ((int)text.length() > 0){
            int width = getWidth(), height = getHeight();
            if(width > 0 && height > 0){
                FCFont *font = getFont();
                Long linkColor = getPaintingLinkColor();
                FCSize tSize = paint->textSize(text, font, -1);
                FCPoint tPoint ={(width - tSize.cx) / 2, (height - tSize.cy) / 2};
                FCPadding padding = getPadding();
                switch (m_textAlign){
                    case FCContentAlignment_BottomCenter:
                        tPoint.y = height - tSize.cy;
                        break;
                    case FCContentAlignment_BottomLeft:
                        tPoint.x = padding.left;
                        tPoint.y = height - tSize.cy - padding.bottom;
                        break;
                    case FCContentAlignment_BottomRight:
                        tPoint.x = width - tSize.cx - padding.right;
                        tPoint.y = height - tSize.cy - padding.bottom;
                        break;
                    case FCContentAlignment_MiddleLeft:
                        tPoint.x = padding.left;
                        break;
                    case FCContentAlignment_MiddleRight:
                        tPoint.x = width - tSize.cx - padding.right;
                        break;
                    case FCContentAlignment_TopCenter:
                        tPoint.y = padding.top;
                        break;
                    case FCContentAlignment_TopLeft:
                        tPoint.x = padding.left;
                        tPoint.y = padding.top;
                        break;
                    case FCContentAlignment_TopRight:
                        tPoint.x = width - tSize.cx - padding.right;
                        tPoint.y = padding.top;
                        break;
                }
                FCRect tRect ={tPoint.x, tPoint.y, tPoint.x + tSize.cx, tPoint.y + tSize.cy};
                Long textColor = getPaintingTextColor();
                if(autoEllipsis() && (tRect.right > clipRect.right || tRect.bottom > clipRect.bottom)){
                    if (tRect.right > clipRect.right) {
                        tRect.right = clipRect.right;
                    }
                    if (tRect.bottom > clipRect.bottom) {
                        tRect.bottom = clipRect.bottom;
                    }
                    paint->drawTextAutoEllipsis(text, linkColor, font, tRect);
                }
                else{
                    paint->drawText(text, linkColor, font, tRect, -1);
                }
                FCNative *native = getNative();
                if (m_linkBehavior == LinkBehaviorA_AlwaysUnderLine || (m_linkBehavior == LinkBehaviorA_HoverUnderLine && this == native->getHoveredView())){
                    paint->drawLine(linkColor, 1, 0, tPoint.x, tPoint.y + tSize.cy, tPoint.x + tSize.cx, tPoint.y + tSize.cy);
                }
            }
        }
    }
    
    void FCLinkLabel::setAttribute(const String& name, const String& value){
        if (name == L"activelinkcolor"){
            setActiveLinkColor(FCTran::strToColor(value));
        }
        else if (name == L"disabledlinkcolor"){
            setDisabledLinkColor(FCTran::strToColor(value));
        }
        else if (name == L"linkbehavior"){
            String lowerStr = FCTran::toLower(value);
            if (lowerStr == L"alwaysunderline"){
                setLinkBehavior(LinkBehaviorA_AlwaysUnderLine);
            }
            else if (lowerStr == L"hoverunderline"){
                setLinkBehavior(LinkBehaviorA_HoverUnderLine);
            }
            else{
                setLinkBehavior(LinkBehaviorA_NeverUnderLine);
            }
        }
        else if (name == L"linkcolor"){
            setLinkColor(FCTran::strToColor(value));
        }
        else if (name == L"linkvisited"){
            setLinkVisited(FCTran::strToBool(value));
        }
        else if (name == L"visitedlinkcolor"){
            setVisitedLinkColor(FCTran::strToColor(value));
        }
        else{
            FCLabel::setAttribute(name, value);
        }
    }
}
